<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="10" :lg="11" :md="12" :sm="24">
            <a-form-item :label="$t('业务时间')">
              <j-date
                :show-time="{ defaultValue: moment('00:00:00', 'HH:mm:ss') }"
                valueFormat="YYYY-MM-DD HH:mm:ss"
                dateFormat="YYYY-MM-DD HH:mm:ss"
                :placeholder="$t('请选择开始时间')"
                class="query-group-cust"
                v-model="queryParam.createTime_begin"
              ></j-date>
              <span class="query-group-split-cust"></span>
              <j-date
                :show-time="{ defaultValue: moment('23:59:59', 'HH:mm:ss') }"
                valueFormat="YYYY-MM-DD HH:mm:ss"
                dateFormat="YYYY-MM-DD HH:mm:ss"
                :placeholder="$t('请选择结束时间')"
                class="query-group-cust"
                v-model="queryParam.createTime_end"
              ></j-date>
            </a-form-item>
          </a-col>
          <a-col :xl="5" :lg="6" :md="7" :sm="24">
            <a-form-item :label="$t('会员信息')">
              <a-input :placeholder="$t('请输入手机号/卡号/姓名')" v-model="queryParam.memberInfo"></a-input>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('设备名称(编码)')">
                <a-select show-search :filter-option="false" @search="handleSearch" v-model="queryParam.deviceId">
                  <a-select-option :value="item.id" :key="index" v-for="(item, index) in deviceList">
                    <span>{{ item.name }}</span>
                    <span v-if="item.name != $t('全部')">({{ item.id }})</span>
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('操作员')">
                <a-select v-model="queryParam.createBy">
                  <a-select-option v-for="(item, index) in userList" :key="index" :value="item.value">
                    {{ item.title }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('票信息')">
                <a-input :placeholder="$t('请输入票号/票名称')" v-model="queryParam.ticketInfo"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item :label="$t('消费类型')">
                <j-dict-select-tag
                  :placeholder="$t('请选择消费类型')"
                  v-model="queryParam.type"
                  dictCode="bis_member_ticket_use.type"
                />
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24" v-if="!tenantid">
              <a-form-item :label="$t('门店')">
                <a-select v-model="queryParam.shopCode">
                  <a-select-option v-for="(item, i) in shops" :key="i" :value="item.value"
                  >{{ item.title }}<span v-if="item.value != ''">（{{ item.value }}）</span></a-select-option
                  >
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">{{ $t('查询') }}</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">{{
                $t('重置')
              }}</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? $t('收起') : $t('展开') }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
              <a-button
              v-has="'BisMemberTicketUseController:export'"
                type="primary"
                icon="download"
                :loading="exloading"
                style="margin-left: 8px"
                @click="handleExportXls($t('门票消费记录'))"
                >{{$t('导出')}}</a-button
              >
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: true }"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :customHeaderRow="customHeaderRow"
        :customRow="customRow"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">{{ $t('无图片') }}</span>
          <img
            v-else
            :src="getImgView(text)"
            height="25px"
            alt=""
            style="max-width: 80px; font-size: 12px; font-style: italic"
          />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">{{ $t('无文件') }}</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
            {{ $t('下载') }}
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">{{ $t('更多') }} <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">{{ $t('详情') }}</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

        <template slot="footer">
          <div style="font-weight: bold">
            <span style="margin-left: 15px; margin-right: 5px">{{ $t('业务笔数') }}：</span>
            <span>{{ totalInfo.count.toFixed(0) }}</span>
            <span style="margin-left: 15px; margin-right: 5px">{{ $t('扣票次数') }}：</span>
            <span>{{ totalInfo.number.toFixed(0) }}</span>
          </div>
        </template>
      </a-table>
    </div>
  </a-card>
</template>

<script>
import moment from 'moment'
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { initDictOptions, filterMultiDictText } from '@/components/dict/JDictSelectUtil'
import { getAction } from '@/api/manage'
import Vue from 'vue'
export default {
  name: 'BisMemberTicketUseList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {},
  data() {
    return {
      description: '门票消费记录管理页面',
      moment,
      tenantid: Vue.ls.get('TENANT_ID'),
      queryParam: {
        column: 'createTime',
        order: 'desc',
        deviceId: '',
        shopCode: '',
        createBy: '',
      },
      // 表头
      columns: [
        {
          title: this.$t('业务时间'),
          align: 'center',
          dataIndex: 'createTime',
        },
        {
          title: this.$t('消费类型'),
          align: 'center',
          dataIndex: 'type_dictText',
        },
        {
          title: this.$t('手机号'),
          align: 'center',
          dataIndex: 'memberPhone',
        },
        {
          title: this.$t('卡号'),
          align: 'center',
          dataIndex: 'chipNo',
        },
        {
          title: this.$t('姓名'),
          align: 'center',
          dataIndex: 'memberName',
        },
        {
          title: this.$t('票号'),
          align: 'center',
          dataIndex: 'ticketId_dictText',
        },
        {
          title: this.$t('票名称'),
          align: 'center',
          dataIndex: 'goodsName',
        },
        {
          title: this.$t('交易前次数'),
          align: 'right',
          dataIndex: 'beforeNumber',
          customRender: (text, record) => {
            if (record.form == 4) {
              return this.$t('不限次')
            } else {
              return text.toFixed(0)
            }
          },
        },
        {
          title: this.$t('扣票次数'),
          align: 'right',
          dataIndex: 'number',
          customRender: (text, r) => {
            return r.number.toFixed(0);
          }
        },
        {
          title: this.$t('交易后次数'),
          align: 'right',
          dataIndex: 'afterNumber',
          customRender: (text, record) => {
            if (record.form == 4) {
              return this.$t('不限次')
            } else {
              return text.toFixed(0);
            }
          },
        },
        {
          title: this.$t('交易前有效期至'),
          align: 'center',
          dataIndex: 'validityEnd',
          customRender: (text, record) => {
            if (!text) {
              return this.$t('不限期')
            } else {
              return text
            }
          },
        },
        {
          title: this.$t('交易后有效期至'),
          align: 'center',
          dataIndex: 'validityEndAfter',
          customRender: (text, record) => {
            if (!text) {
              return this.$t('不限期')
            } else {
              return text
            }
          },
        },
        {
          title: this.$t('设备名称(编码)'),
          align: 'center',
          dataIndex: 'deviceId_dictText',
          customRender: function (text, record) {
            if (text) {
              return record.deviceId_dictText + '（' + record.deviceId + '）'
            }
            return ''
          },
        },
        {
          title: this.$t('操作员'),
          align: 'center',
          dataIndex: 'createBy',
        },
        {
          title: this.$t('关联年月次票'),
          align: 'center',
          dataIndex: 'parentId_dictText',
        },
        {
          title: this.$t('备注'),
          align: 'center',
          dataIndex: 'comment',
        },
      ],
      url: {
        list: '/member/bisMemberTicketUse/list',
        delete: '/member/bisMemberTicketUse/delete',
        deleteBatch: '/member/bisMemberTicketUse/deleteBatch',
        exportXlsUrl: '/member/bisMemberTicketUse/exportXls',
        importExcelUrl: 'member/bisMemberTicketUse/importExcel',
        totalUrl: 'member/bisMemberTicketUse/total',
      },
      dictOptions: {},
      superFieldList: [],
      deviceList: [],
      userList: [],
      shops: [],
      isfooter: true,
      totalInfo: {
        count: 0,
        number: 0,
      },
    }
  },
  created() {
    if (!this.tenantid) {
      let dtl = {
          title: this.$t('门店编码'),
          align: 'center',
          dataIndex: 'shopCode',
          width: 100,
          customCell: (r, i) => {
            return {
              style: {
                whiteSpace: 'normal',
              },
            }
          },
        },
        dtl1 = {
          title: this.$t('门店名称'),
          align: 'center',
          dataIndex: 'shopCode_dictText',
          width: 140,
          customCell: (r, i) => {
            return {
              style: {
                whiteSpace: 'normal',
              },
            }
          },
        }
      this.columns.push(dtl1)
      this.columns.push(dtl)
    }
    this.getSuperFieldList()
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  methods: {
    initDictConfig() {
      initDictOptions('sys_user,realname,realname').then((res) => {
        if (res.success) {
          this.userList = res.result
          this.userList.unshift({ title: this.$t('全部'), value: '' })
        }
      })
      initDictOptions('bis_shop,name,code').then((res) => {
        if (res.success) {
          this.shops = res.result
          this.shops.unshift({ value: '', title: this.$t('全部') })
        }
      })
      this.handleSearch('')
    },
    handleSearch(value) {
      let query = {}
      if (value) {
        query.deviceInfo = value
      }
      getAction('/device/bisDeviceInfo/list', query).then((res) => {
        if (res.success) {
          this.deviceList = res.result.records
          this.deviceList.unshift({ id: '', name: this.$t('全部') })
        }
      })
    },
    searchReset(){
      this.queryParam = {
        deviceId: '',
        createBy: '',
        shopCode: '',
      }
      this.selectedRowIndex = []
      this.loadData(1);
    },
    getSuperFieldList() {
      let fieldList = []
      fieldList.push({ type: 'datetime', value: 'createTime', text: this.$t('核销时间') })
      fieldList.push({ type: 'string', value: 'memberId', text: this.$t('会员ID'), dictCode: '' })
      fieldList.push({ type: 'string', value: 'ticketId', text: this.$t('门票ID'), dictCode: '' })
      fieldList.push({ type: 'BigDecimal', value: 'number', text: this.$t('核销次数'), dictCode: '' })
      fieldList.push({
        type: 'string',
        value: 'deviceId',
        text: this.$t('设备ID'),
        dictCode: 'bis_device_info,name,id',
      })
      fieldList.push({
        type: 'string',
        value: 'createBy',
        text: this.$t('操作员'),
        dictCode: 'sys_user,realname,username',
      })
      this.superFieldList = fieldList
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>